<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户">
        <el-input v-model="queryParams.createBy"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userVipList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户" align="center" prop="createBy" />
      <el-table-column label="使用积分" align="center" prop="totalPointNum" >
        <template #default="scope">
          {{scope.row.totalPointNum - scope.row.pointNum}}
        </template>
      </el-table-column>
      <el-table-column label="积分" align="center" prop="pointNum" />
      <el-table-column label="到期时间" align="center" prop="expireTime" />
      <el-table-column label="操作" align="center" min-width="150">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleBack(scope.row)"
            v-hasPermi="['work:userVip:back']"
          >退款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listUserVip, getUserVip, delUserVip, addUserVip, updateUserVip, backUserVip} from "@/api/work/userVip";

export default {
  name: "UserVip",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户套餐表格数据
      userVipList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        pointNum: null,
        expireTime: null
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户套餐列表 */
    getList() {
      this.loading = true;
      listUserVip(this.queryParams).then(response => {
        this.userVipList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleBack(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认退款改用户？').then(function() {
        return backUserVip(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('work/userVip/export', {
        ...this.queryParams
      }, `userVip_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
